<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js">/*
 * Copyright (c) 2016 VUI(https://git.oschina.net/durcframework/vui) All rights reserved.
 */

;(function(){

<span id='VUI-PageForm'>/**
</span> * 页面表单控件,包含了增删改查功能&lt;br&gt;
 &lt;pre&gt;
pageForm = new VUI.PageForm({
	formId:'form'
	,grid:grid
	,win:win
	,crudUrl:{
		add:addUrl
	}
});
 &lt;/pre&gt;
 * @class VUI.PageForm
 * @extends VUI.Form
 */
VUI.Class('PageForm',{
	// 默认属性
	OPTS:{
		grid:null
		,win:null
		,crudUrl:null
	}
<span id='global-method-constructor'>	/**
</span>	 * 构造函数
	 * @constructor
	 * @ignore
	 */
	,init:function(opts) {
		this._super(opts);
	}
<span id='VUI-PageForm-method-add'>	/**
</span>	 * 添加
	 */
	,add:function() {
		this.setUrl(this.getCrudUrl().add);
		this.clear();
		this.getWin().setTitle('添加');
		this.getWin().show();
	}
<span id='VUI-PageForm-method-update'>	/**
</span>	 * 修改
	 * @param {Object} data 表格行数据
	 */
	,update:function(data) {
		this.getWin().setTitle('修改');
		this.setUrl(this.getCrudUrl().update);
		this.clear();
		this.loadData(data);
		this.getWin().show();
	}
<span id='VUI-PageForm-method-del'>	/**
</span>	 * 删除
	 * @param {Object} data 表格行数据
	 */
	,del:function(data) {
		var self = this;
		VUI.Msg.confirm('确认','确定删除这条记录?',function(r){
			if(r){
		    	var url = self.getCrudUrl().del;
				self.submit({
					url:url
					,data:data
					,onSubmit:function(){}
					,success: function(data){
						if(data.success) {							
							self.getGrid().reload();							
						}else{
							VUI.Msg.alert('提示',data.message);
						}
					}
				});
			}
		});
	}
<span id='VUI-PageForm-method-save'>	/**
</span>	 * 保存
	 * @param {String} opts 保存选项&lt;br&gt;
	 * {onSubmit:function(data){},success:function:(data){}}
	 */
	,save:function(opts) {
		opts = opts || {}
		var self = this;
		var url = this.getUrl();
		this.submit({
			url:url
			,onSubmit: function(data){
				if(opts.onSubmit) {
					return opts.onSubmit.call(self,data);
				}else{
					return this.validate();	// 返回false终止表单提交
				}
			}
			,success: function(data){
				if(data.success) {
					if(opts.success) {
						return opts.success.call(self,data);
					}else{
						self.getGrid().reload();
						self.getWin().close();
					}
				}else{
					VUI.Msg.alert('提示',data.message);
				}
			}
		});
		
	}
<span id='VUI-PageForm-method-setUrl'>	/**
</span>	 * 设置提交的url
	 * @param {String} url url
	 */
	,setUrl:function(url) {
		this.opt('url',url);
	}
<span id='VUI-PageForm-method-getUrl'>	/**
</span>	 * 返回提交的url
	 * @return 提交的url
	 */
	,getUrl:function() {
		return this.opts.url;
	}
<span id='VUI-PageForm-method-getCrudUrl'>	/**
</span>	 * 获取增删改查的url
	 * @return 返回一个json对象,如:
	 * {
		add:ctx + '/addStudent.do'
		,update:ctx + '/updateStudent.do'
		,del:ctx + '/delStudent.do'
		}
	 */
	,getCrudUrl:function() {
		return this.opts.crudUrl;
	}
<span id='VUI-PageForm-method-getWin'>	/**
</span>	 * 获取win控件
	 */
	,getWin:function() {
		return this.opts.win;
	}
<span id='VUI-PageForm-method-getGrid'>	/**
</span>	 * 获取grid控件
	 */
	,getGrid:function() {
		return this.opts.grid;
	}
},VUI.Form);

})();</pre>
</body>
</html>
